package view;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.event.ChangeListener;

import tabbedCanvas.LayeredCanvas;
import util.resources.ResourceManager;

/**
 * Creates tabbed panel to display workspaces. Utilizes property
 * files to change properties easily.
 * 
 * Modified from VariableTabbedPane.java (Picassa Team 6)
 * 
 * @author Wesley Brown
 * @author Ethan Yong-Hui Goh
 */

public class TabbedPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTabbedPane tabPane = new JTabbedPane();
	private ResourceManager resources;
	private int tabIndex = 0;
	private Dimension d;
	private String defaultTabName;
	private List<LayeredCanvas> paneList = new ArrayList<LayeredCanvas>();

	/**
	 * Create tabbed panel and populate it with default tabs.
	 * 
	 * @param resources ResourceManager linked with property file containing
	 * information about the tabbed panel.
	 * 
	 */
	public TabbedPanel(ResourceManager resources, ChangeListener controller) {
		this.resources = resources;
		this.setLayout(new GridLayout(1, 1));
		int[] panelSize = resources.getIntegerArray("tabPanelSize");
		d = new Dimension(panelSize[0], panelSize[1]);
		this.setPreferredSize(d);
		defaultTabName = resources.getString("defaultTabName");
		// Create "New Tab" button
		createNewTabButton();

		// Create default workspace tabs
		for (int i = 0; i < resources.getInteger("numTabs"); i++) {
		    LayeredCanvas lp = new LayeredCanvas(d, i);
			paneList.add(lp);
			createTab(lp);
		}

		tabPane.addChangeListener(controller);
		// Select the first tab
		tabPane.setSelectedIndex(0);
		this.add(tabPane);
	}

	/**
	 * Create a new tab containing a specified component.
	 * 
	 * @param o Component to contain within in tab.
	 */
	private void createTab(JComponent o) {
		tabIndex++;
		tabPane.insertTab(defaultTabName + tabIndex,
				null, o, null, tabPane.getTabCount() - 1);

		// Create as next to last tab, since last tab is the '+' button
		createTabCloseButton(tabPane.getTabCount() - 2);
		tabPane.setSelectedIndex(tabPane.getTabCount() - 2);
	}

	/**
	 * Add close button to a specified tab.
	 * 
	 * @param i Tab index
	 */
	private void createTabCloseButton(int i) {
		tabPane.setTabComponentAt(i, new ButtonTabComponent(tabPane));
	}

	/**
	 * Create "New Tab" button.
	 */
	private void createNewTabButton() {
		tabPane.addTab("", new JPanel());

		JButton addNewTab = new JButton(" + ");
		addNewTab.setMargin(new Insets(0, 0, 0, 0));

		ActionListener al;

		al = new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
			    LayeredCanvas lp = new LayeredCanvas(d, tabIndex+1);
				paneList.add(lp);
				createTab(lp);
			}
		};
		addNewTab.addActionListener(al);

		tabPane.setTabComponentAt(tabPane.getTabCount() - 1, addNewTab);
		tabPane.setSelectedIndex(tabPane.getTabCount() - 2);
	}

	/**
	 * @return Get list of layered panes containing drawn vectors.
	 */
	public List<LayeredCanvas> getLayeredCanvas() {
		return paneList;
	}

}
